#include "board.h"

Board::~Board(){
    int i = topicPool.count();
    while ( i -- > 0)
	delete topicPool[i];
    topicPool.clear();
}

Board::Board(QString newId , QString newName )
:id(newId),name(newName){
    favorite = false;
    profile = 0;
}

bool Board::addTopic (Topic * topic){
    bool ret = true;
    int i = topicPool.count();
    while ( i -- > 0)
	if (topic->id == topicPool[i]->id)
	    break;
	if (i == -1){
	    topicPool << topic;
// 	    profile->notice(topic);
	}
	else
	    ret =  false;
	return ret;
}

QString Board::display(){
    return name+"["+id+"]";
}

// defines for the board model

boardModel::boardModel(QObject *parent) : QAbstractListModel(parent), board(0){}

QVariant boardModel::data(const QModelIndex &index, int role) const{
    if (!index.isValid() || !board)
	return QVariant();
    if (index.row() >= board->topicPool.count())
	return QVariant();
    if (role == Qt::DisplayRole)
	return board->topicPool.at(index.row())->display();
    else
	return QVariant();
}

int boardModel::rowCount(const QModelIndex &) const{
    if (board == 0)
	return 0;
    else
	return board->topicPool.count();
}

void boardModel::setBoard(Board * newBoard){
    board = newBoard;
    connect(&gTimer, SIGNAL(timeout()), board, SLOT(refreshMe()));
    connect(board, SIGNAL(refreshFinished(bool)), this, SLOT(refresh(bool)));
    Q_ASSERT(board != 0);
    board->refreshMe();
}

void boardModel::refresh(bool){
    reset();
}

QModelIndex boardModel::index(int row, int column, const QModelIndex &parent) const{
    if (parent.isValid() || board == 0 || row < 0)
	return QModelIndex();
    
    if (row >= board->topicPool.count())
	return QModelIndex();
    
    Topic * topic = board->topicPool.value(row);
    return createIndex(row, column, topic);
}
